package com.bbx.taxi.client.Adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.passanger.Return.PriceDetail;
import com.bbx.api.sdk.model.passanger.Return.CarType.CarType;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.R.color;
import com.bbx.taxi.client.Activity.Main.MainActivity;
import com.bbx.taxi.client.Activity.Menu.SetActivity;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity2;
import com.bbx.taxi.client.Activity.Order.StartServiceActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute.CjType;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.Util.AgainOrder;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.Tel;
import com.bbx.taxi.client.widget.MyViewPager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyOrderAdapter extends BaseAdapter {
	private ArrayList<MyOrderList> list;
	private Activity context;
	private boolean isItemClick = false;
	private MyViewPager mViewPager;
	private int requestCode;
	private LineDB db_line;// 线路数据库

	public MyOrderAdapter(Activity context, ArrayList<MyOrderList> list,
			MyViewPager mViewPager) {
		this.list = list;
		this.mViewPager = mViewPager;
		this.context = context;
		db_line = new LineDB();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	public boolean getItemClick() {
		return isItemClick;
	}

	public void setItemClick(boolean isItemClick) {
		this.isItemClick = isItemClick;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = (View) inflater.inflate(R.layout.listitem_myorder,
					null);
			viewholder = new ViewHolder();
			viewholder.status = (TextView) convertView
					.findViewById(R.id.status);
			viewholder.time = (TextView) convertView.findViewById(R.id.time);
			viewholder.order_type = (TextView) convertView
					.findViewById(R.id.order_type);
			viewholder.start_address = (TextView) convertView
					.findViewById(R.id.start_address);
			viewholder.end_address = (TextView) convertView
					.findViewById(R.id.end_address);
			viewholder.tv_price = (TextView) convertView
					.findViewById(R.id.tv_price);
			viewholder.ring_up = (ImageView) convertView
					.findViewById(R.id.ring_up);
			viewholder.layout_item = (LinearLayout) convertView
					.findViewById(R.id.layout_item);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
		showPrice(viewholder, position);
		viewholder.time.setText(list.get(position).getAppoint_time());
		viewholder.order_type.setText(getType(list.get(position).getOrder_type(),list.get(position).getOrder_origin()));
		viewholder.start_address
				.setText(list.get(position).getLocations().start.address);
		viewholder.end_address
				.setText(list.get(position).getLocations().end.address);
		getPhoneType(viewholder.ring_up, position);
		getStatus(viewholder.status, list.get(position).getOrder_status(),
				Integer.parseInt(list.get(position).getValuate_status()));
		viewholder.layout_item.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mViewPager.setNoScroll(true);

				return false;
			}
		});
		setOrder_origin(viewholder,position);
		return convertView;

	}

	public class ViewHolder {
		TextView time, order_type, start_address, end_address, status,
				tv_price;
		ImageView ring_up;// 打电话
		LinearLayout layout_item;
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				// context.startActivity(new Intent(context,
				// AddAddressActivity.class));
				break;
			}
		};
	};

	/**
	 * 类型的获取。拼车，包车，寄件
	 */
	public String getType(int order_type,int order_origin) {
		String str="";
		switch (order_type) {
		case Value.ORDER_TYPE_PC:
			str= context.getString(R.string.pc);
			break;
		case Value.ORDER_TYPE_BC:
			str= context.getString(R.string.bc);
			break;
		case Value.ORDER_TYPE_KJ:
			str= context.getString(R.string.kj);
			break;
		case Value.ORDER_TYPE_SN:
			str= context.getString(R.string.sn);
			break;
		}
		switch(order_origin){
		case Value.ORDER_ORIGIN_SF:
			str= context.getString(R.string.sf);
			break;
		case Value.ORDER_ORIGIN:
			str= context.getString(R.string.aixin);
			break;
		}
		return str;
	}

	/**
	 * “order_status”:0:已提交,1:已派单,2:已结束 获取电话类型，，；联系客服，联系司机，去评价
	 */
	public void getPhoneType(ImageView v, final int position) {
		String toast = null;
		switch (list.get(position).getOrder_status()) {
		case Value.ORDER_STATUS_UNTREATED:// 未处理
		case Value.ORDER_STATUS_TEAM:// 已入队列
		case Value.ORDER_STATUS_RETEAM:// 重新调度入队列
		case Value.ORDER_STATUS_GAIPAI:// 改派
		case Value.ORDER_STATUS_KF_GAIPAI:// 改派
			v.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.selector_kftel));
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Tel.onTel_KF(context, list.get(position));
				}
			});
			break;
		case Value.ORDER_STATUS_ASSiIGN:// 已指派
		case Value.ORDER_STATUS_ONCAR:// 已上车
			v.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.selector_sjtel));
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// 根据viewId判断司机的电话经行拨打
					ToastUtil.showToast(context, R.string.tel_si);
					// 用intent启动拨打电话
					context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
							.parse("tel:"
									+ list.get(position).getDriver_phone())));
				}
			});
			break;
		case Value.ORDER_STATUS_OFFCAR:// 已下车
			v.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.selector_pay));
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							OrderEvaluationActivity2.class);
					intent.putExtra(OrderMsg.order_id_finish,
							list.get(position).order_id);
					intent.putExtra(OrderMsg.finish_type, OrderMsg.finish_ing);
					context.startActivityForResult(intent, requestCode);
				}
			});
			break;
		case Value.ORDER_STATUS_FINISH:// 已完成
			// 待评价
			if (Integer.parseInt(list.get(position).getValuate_status()) == Value.VALUATE_STATUS_NO) {
				v.setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.selector_pj));
				v.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context,
								OrderEvaluationActivity2.class);
						intent.putExtra(OrderMsg.order_id_finish,
								list.get(position).order_id);
						intent.putExtra(OrderMsg.finish_type,
								OrderMsg.finish_all);
						context.startActivityForResult(intent, requestCode);
					}
				});
			} else {
				v.setBackgroundDrawable(context.getResources().getDrawable(
						R.drawable.selector_zcxd));
				v.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						MainAttribute main = MainAttribute.getInstance();
						switch (list.get(position).getOrder_type()) {
						case Value.ORDER_TYPE_SN:
							AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_SN);
							break;
						case Value.ORDER_TYPE_KJ:
							AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_KJ);
							break;
						case Value.ORDER_TYPE_PC:
							if(list.get(position).getOrder_origin()==Value.ORDER_ORIGIN_SF){
								main.setCjType(CjType.SF);
								AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_PC);
							}else{
								main.setCjType(CjType.PC);
								AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_PC);
							}
							break;
						case Value.ORDER_TYPE_BC:
							main.setCjType(CjType.BC);
							AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_PC);
							break;
						}
					}
				});
			}
			break;
		case Value.ORDER_STATUS_CANCEL:// 已取消
		case Value.ORDER_STATUS_KF_CANCEL:// 已取消
		case Value.ORDER_STATUS_GAIPAI_CANCEL:// 改派取消
			v.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.selector_zcxd));
			v.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.selector_zcxd));
			v.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					MainAttribute main = MainAttribute.getInstance();
					switch (list.get(position).getOrder_type()) {
					case Value.ORDER_TYPE_SN:
						AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_SN);
						break;
					case Value.ORDER_TYPE_KJ:
						AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_KJ);
						break;
					case Value.ORDER_TYPE_PC:
						if(list.get(position).getOrder_origin()==Value.ORDER_ORIGIN_SF){
							main.setCjType(CjType.SF);
							AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_PC);
						}else{
							main.setCjType(CjType.PC);
							AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_PC);
						}
						break;
					case Value.ORDER_TYPE_BC:
						main.setCjType(CjType.BC);
						AgainOrder.onAgainOrder(context, list.get(position), Value.VIEWID_PC);
						break;
					}
				}
			});
			break;
		default:
			v.setVisibility(View.GONE);
			break;
		}
	}

	/**
	 * “order_status”:0:已提交,1:已派单,2:已结束 获取电话类型，，；等待派车，服务中，待评价
	 */
	public void getStatus(TextView v, int order_status, int valuate_status) {
		switch (order_status) {
		case Value.ORDER_STATUS_UNTREATED:// 未处理
		case Value.ORDER_STATUS_TEAM:// 已入队列
		case Value.ORDER_STATUS_RETEAM:// 重新调度入队列
			v.setTextColor(context.getResources().getColor(R.color.btn_nor));
			v.setText(R.string.order_status_wait);
			break;
		case Value.ORDER_STATUS_GAIPAI:// 改派
		case Value.ORDER_STATUS_KF_GAIPAI:// 改派
			v.setTextColor(context.getResources().getColor(R.color.btn_nor));
			v.setText(R.string.order_status_wait);
			break;
		case Value.ORDER_STATUS_ASSiIGN:// 已指派
		case Value.ORDER_STATUS_ONCAR:// 已上车
			v.setTextColor(context.getResources().getColor(R.color.btn_nor));
			v.setText(R.string.order_status_service);
			break;
		case Value.ORDER_STATUS_OFFCAR:// 已下车
			v.setTextColor(context.getResources().getColor(R.color.light_red));
			v.setText(R.string.order_status_pay);
			break;
		case Value.ORDER_STATUS_FINISH:// 已完成
			// 待评价
			if (valuate_status == Value.VALUATE_STATUS_NO) {
				v.setTextColor(context.getResources().getColor(R.color.black));
				v.setText(R.string.order_status_no_evaluat);
				break;
			} else {
				v.setTextColor(context.getResources().getColor(R.color.black));
				v.setText(R.string.order_status_finish);
				break;
			}
		case Value.ORDER_STATUS_CANCEL:// 已取消
		case Value.ORDER_STATUS_KF_CANCEL:// 已取消
		case Value.ORDER_STATUS_GAIPAI_CANCEL:// 已取消
			v.setTextColor(context.getResources().getColor(R.color.black));
			v.setText(R.string.order_status_cancel);
			break;
		default:
			v.setTextColor(context.getResources().getColor(R.color.black));
			v.setText(R.string.order_status_finish);
			break;
		}

	}

	/**
	 * 根据不同类型显示不同价格
	 */
	public void showPrice(ViewHolder viewholder, int position) {
		int type = list.get(position).getOrder_type();
		int status = list.get(position).getOrder_status();

		// 小于待支付，显示原价金额
		if (status < Value.ORDER_STATUS_OFFCAR) {
			viewholder.tv_price.setText(String.format(
					context.getString(R.string.orderlist_price),
					FormatUtil.onFormatPrice(list.get(position).getPrice())));
			viewholder.tv_price.setVisibility(View.VISIBLE);
			if (type == Value.ORDER_TYPE_SN){
				viewholder.tv_price.setVisibility(View.GONE);
			}
		}
		// 待支付，显示具体司机给的价格price_detail
		else if (status == Value.ORDER_STATUS_OFFCAR) {
			PriceDetail price_detail = list.get(position).getPrice_Detail();
			double price=0;
			if (price_detail != null){
				price=price_detail.getTotal();
			}else{
				price=list.get(position).getPrice();
			}
			viewholder.tv_price.setVisibility(View.VISIBLE);
			viewholder.tv_price.setText(String.format(
					context.getString(R.string.orderlist_price),
					FormatUtil.onFormatPrice(price)));
		}
		// 显示实际金额
		else {
			viewholder.tv_price.setVisibility(View.VISIBLE);
			double actual_price=-1;
			actual_price=list.get(position).getActual_price();
			//如果actual_price没有值，则取price_detail
			if(list.get(position).getPrice_Detail()!=null&&actual_price==-1){
				actual_price=list.get(position).getPrice_Detail().getActual_price();
			}
			if (actual_price != -1) {
				viewholder.tv_price.setText(String.format(context
						.getString(R.string.orderlist_price), FormatUtil
						.onFormatPrice(actual_price)));
			} else {
				viewholder.tv_price.setText(String.format(context
						.getString(R.string.orderlist_price), FormatUtil
						.onFormatPrice(list.get(position).getTotal_price())));
			}
		}
		// 已取消,（公式计价，并且订单还没下车）不显示
		int calc_type=list.get(position).getCalcType();
		if (status == Value.ORDER_STATUS_CANCEL
				|| status == Value.ORDER_STATUS_KF_CANCEL
				|| status == Value.ORDER_STATUS_GAIPAI_CANCEL||(calc_type==CarType.CALC_TYPE_jifei&&status < Value.ORDER_STATUS_OFFCAR)) {
			viewholder.tv_price.setVisibility(View.GONE);
	    }
     }
	public void setOrder_origin(ViewHolder viewholder,int position){
		int order_origin=list.get(position).getOrder_origin();
		int order_status=list.get(position).getOrder_status();
		viewholder.tv_price.setVisibility(viewholder.tv_price.getVisibility());
		  viewholder.ring_up.setVisibility(View.VISIBLE);
		
		if(order_origin==Value.ORDER_ORIGIN){
			viewholder.tv_price.setVisibility(View.GONE);
			viewholder.order_type.setText(context.getString(R.string.aixin));
		
		switch(order_status){
		  case Value.ORDER_STATUS_FINISH:
			  if (Integer.parseInt(list.get(position).getValuate_status()) == Value.VALUATE_STATUS) {
				  viewholder.ring_up.setVisibility(View.GONE);
			  }else{
				  viewholder.ring_up.setVisibility(View.VISIBLE);
			  }
			  break;
		  case Value.ORDER_STATUS_CANCEL:// 已取消
		  case Value.ORDER_STATUS_KF_CANCEL:// 已取消
		  case Value.ORDER_STATUS_GAIPAI_CANCEL:// 改派取消
			  viewholder.ring_up.setVisibility(View.GONE);
			  break;
			case Value.ORDER_STATUS_UNTREATED:// 未处理
			case Value.ORDER_STATUS_TEAM:// 已入队列
			case Value.ORDER_STATUS_RETEAM:// 重新调度入队列
			case Value.ORDER_STATUS_GAIPAI:// 改派
			case Value.ORDER_STATUS_KF_GAIPAI:// 改派
				viewholder.ring_up.setVisibility(View.GONE);
				break;
		  default:
			  viewholder.ring_up.setVisibility(View.VISIBLE);
				  break;
		}
		}
	}
}
